package com.reyco1.medusa.display.control
{
	import com.reyco1.medusa.display.SmartSprite;
	import com.reyco1.medusa.display.container.Container;
	import com.reyco1.medusa.display.graphics.Box;
	import com.reyco1.medusa.manager.DragManager;
	import com.reyco1.medusa.util.Delegate;
	import com.reyco1.medusa.util.MathUtil;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	public class HSlider extends BaseControl
	{
		private var _lineColor:uint 	= 0x000000;
		private var _fillColor:uint 	= 0xFFFFFF;
		private var _lineThickness:int  = 2;		
		private var _percent:Number		= 0;
		private var _thumbThickness:int = 10;
		
		private var track:SmartSprite;
		private var thumb:SmartSprite;
		
		private var updateHandler:Function;
		private var updateParams:Array;
		
		public function HSlider(onUpdate:Function = null, onUpdateParams:Array = null)
		{
			super();
			_width = 100;
			_height = 20;
			updateHandler = onUpdate;
			updateParams = onUpdateParams;
			build()
		}
		
		override protected function build():void
		{
			clear();
			
			track =  Box.draw(-5, -_height * 0.5, _width, _height, 0x000000, 0);
			track.addEventListener(MouseEvent.CLICK, updateThumb);
			track.buttonMode = track.useHandCursor = true;
			track.graphics.moveTo(0, 0);
			track.graphics.lineStyle(_lineThickness, _lineColor);
			track.graphics.lineTo(_width, 0);
			track.graphics.endFill();
			addChild(track);
			
			thumb = Box.draw(-_thumbThickness * 0.5, -_height * 0.5, _thumbThickness, _height, _fillColor, 1, {thickness:_lineThickness, color:_lineColor, alpha:1});
			thumb.buttonMode = thumb.useHandCursor = true;
			addChild(thumb);
			
			DragManager.allowDrag(thumb, new Rectangle(0, 0, _width, 0), false, null, null, null, updatePercent);
			
			dispatchEvent(new Event(Event.RESIZE))
		}
		
		private function updatePercent():void
		{
			_percent = MathUtil.percentage(Math.ceil(thumb.x), Math.ceil(track.x + track.width));
			if(updateHandler != null) Delegate.execute(updateHandler, updateParams);
		}
		
		public function setPercentage(num:Number):void
		{
			_percent = num;
			thumb.x = (track.x + track.width) * _percent;
		}
		
		override public function clear():void
		{
			if(track)
			{
				removeChild(track);
				track.removeAllListeners();
				track = null;
			}
			
			if(thumb)
			{
				DragManager.unallowDrag(thumb);
				thumb.removeAllListeners();
				removeChild(thumb);
				thumb = null;
			}
		}
		
		/* handlers */
		
		private function updateThumb(e:MouseEvent):void
		{
			var mX:Number = track.mouseX;
			var perc:Number = MathUtil.percentage(mX, track.width);
			setPercentage(perc);
			updatePercent();
		}
		
		/* properties */
		
		public function get lineColor():uint
		{
			return _lineColor;
		}
		
		public function set lineColor(value:uint):void
		{
			_lineColor = value;
			invalidate();
		}
		
		public function get fillColor():uint
		{
			return _fillColor;
		}
		
		public function set fillColor(value:uint):void
		{
			_fillColor = value;
			invalidate();
		}
		
		public function get lineThickness():int
		{
			return _lineThickness;
		}
		
		public function set lineThickness(value:int):void
		{
			_lineThickness = value;
			invalidate();
		}
		
		public function get percent():Number
		{
			return _percent;
		}

		public function get thumbThickness():int
		{
			return _thumbThickness;
		}

		public function set thumbThickness(value:int):void
		{
			_thumbThickness = value;
			invalidate();
		}
		
		
	}
}